home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / libs / ttengine.lha / ttengine-4.1 / Examples / TextFit / txfit.c < prev   
C/C++ Source or Header  |  2002-09-29  |  7KB  |  195 lines

  1. /* test ttrender */
  2.  
  3. #define __NOLIBBASE__
  4.  
  5. #include <proto/dos.h>
  6. #include <proto/exec.h>
  7. #include <proto/intuition.h>
  8. #include <proto/graphics.h>
  9. #include <proto/ttengine.h>
  10. #include <proto/asl.h>
  11.  
  12. #include <libraries/ttengine.h>
  13.  
  14. extern struct Library *SysBase, *DOSBase;
  15.  
  16. struct Library *TTEngineBase, *IntuitionBase, *GfxBase, *AslBase;
  17. ULONG Red;
  18.  
  19. /*----------------------------------------------------------------------------------------------------*/
  20.  
  21. static STRPTR get_font_name(struct Library *AslBase)
  22.   {
  23.     struct FileRequester *freq;
  24.     STRPTR name = NULL;
  25.  
  26.     if (freq = AllocAslRequestTags(ASL_FileRequest, TAG_END))
  27.       {
  28.         if (AslRequestTags(freq,
  29.           ASLFR_TitleText, (ULONG)"Select TrueType font",
  30.           ASLFR_InitialDrawer, (ULONG)"FONTS:",
  31.           ASLFR_DoPatterns, TRUE,
  32.           ASLFR_InitialPattern, (ULONG)"#?.ttf",
  33.           ASLFR_RejectIcons, TRUE,
  34.           TAG_END))
  35.           {
  36.             ULONG namelen = strlen(freq->fr_File) + strlen(freq->fr_Drawer) + 4;
  37.  
  38.             if (name = AllocVec(namelen + 1, MEMF_ANY | MEMF_CLEAR))
  39.               {
  40.                 strncpy(name, freq->fr_Drawer, namelen);
  41.                 AddPart(name, freq->fr_File, namelen);
  42.               }
  43.           }
  44.         FreeAslRequest(freq);
  45.       }
  46.     return name;
  47.   }
  48.  
  49. /*----------------------------------------------------------------------------------------------------*/
  50.  
  51. static VOID free_font_name(STRPTR name)
  52.   {
  53.     if (name) FreeVec(name);
  54.   }
  55.  
  56. /*----------------------------------------------------------------------------------------------------*/
  57.  
  58. void fitted_text(STRPTR text, UWORD y, struct RastPort *rp, struct TextExtent *cte)
  59.   {
  60.     struct TextExtent te;
  61.     UWORD len;
  62.     ULONG pixlen, to_draw;
  63.  
  64.     /* draw constraint frame */
  65.  
  66.     SetAPen(rp, Red);
  67.     Move(rp, 20 + cte->te_Extent.MinX, y + cte->te_Extent.MinY);
  68.     Draw(rp, 20 + cte->te_Extent.MaxX, y + cte->te_Extent.MinY);
  69.     Draw(rp, 20 + cte->te_Extent.MaxX, y + cte->te_Extent.MaxY);
  70.     Draw(rp, 20 + cte->te_Extent.MinX, y + cte->te_Extent.MaxY);
  71.     Draw(rp, 20 + cte->te_Extent.MinX, y + cte->te_Extent.MinY);
  72.  
  73.     /* draw text bounding box */
  74.  
  75.     len = strlen(text);
  76.     to_draw = TT_TextFit(rp, text, len, &te, cte, 1, 0, 0);
  77.  
  78.     SetAPen(rp, 2);
  79.     Move(rp, 20 + te.te_Extent.MinX, y + te.te_Extent.MinY);
  80.     Draw(rp, 20 + te.te_Extent.MaxX, y + te.te_Extent.MinY);
  81.     Draw(rp, 20 + te.te_Extent.MaxX, y + te.te_Extent.MaxY);
  82.     Draw(rp, 20 + te.te_Extent.MinX, y + te.te_Extent.MaxY);
  83.     Draw(rp, 20 + te.te_Extent.MinX, y + te.te_Extent.MinY);
  84.  
  85.     /* draw the text itself */
  86.  
  87.     SetAPen(rp, 1);
  88.     Move(rp, 20, y);
  89.     TT_Text(rp, text, to_draw);
  90.   }
  91.  
  92.  
  93. int Main (void)
  94.   {
  95.     struct Window *win;
  96.     STRPTR fontname;
  97.  
  98.     if (GfxBase = OpenLibrary("graphics.library", 39))
  99.       {
  100.         if (IntuitionBase = OpenLibrary("intuition.library", 39))
  101.           {
  102.             if (AslBase = OpenLibrary("asl.library", 38))
  103.               {
  104.                 if (fontname = get_font_name(AslBase))
  105.                   {
  106.                     if (TTEngineBase = OpenLibrary("ttengine.library", 0))
  107.                       {
  108.                         if (win = OpenWindowTags(NULL,
  109.                           WA_Top, 25,
  110.                           WA_Left, 0,
  111.                           WA_Width, 640,
  112.                           WA_Height, 210,
  113.                           WA_CloseGadget, TRUE,
  114.                           WA_DragBar, TRUE,
  115.                           WA_DepthGadget, TRUE,
  116.                           WA_IDCMP, IDCMP_CLOSEWINDOW,
  117.                           WA_Title, (ULONG)"TT_TextFit() test",
  118.                           TAG_END))
  119.                           {
  120.                             ULONG sigmask, signals;
  121.                             BOOL running = TRUE;
  122.                             struct RastPort *rp = win->RPort;
  123.                             APTR font;
  124.  
  125.                             Red = ObtainBestPen(win->WScreen->ViewPort.ColorMap,
  126.                               201, 0, 0, OBP_Precision, PRECISION_IMAGE, TAG_END);
  127.  
  128.                             if (font = TT_OpenFont(
  129.                               TT_FontFile, (ULONG)fontname,
  130.                               TT_FontSize, 16,
  131.                             TAG_END))
  132.                               {
  133.                                 struct TextExtent cte;
  134.                                 WORD i;
  135.  
  136.                                 cte.te_Width = 32767;
  137.                                 cte.te_Height = 32767;
  138.                                 cte.te_Extent.MinX = -10;
  139.                                 cte.te_Extent.MinY = -20;
  140.                                 cte.te_Extent.MaxX = 600;
  141.                                 cte.te_Extent.MaxY = 20;
  142.  
  143.                                 TT_SetFont(rp, font);
  144.                                 TT_SetAttrs(rp,
  145.                                   TT_Window, (ULONG)win,
  146.                                   TT_Antialias, TT_Antialias_On,
  147.                                 TAG_END);
  148.  
  149.                                 SetDrMd(rp, JAM1);
  150.  
  151.                                 for (i = 0; i < 100; i++)
  152.                                   {
  153.                                     fitted_text("TT_TextFit() example shows how the text is fitted into constraining rectangle.", 45, rp, &cte);
  154.                                     cte.te_Extent.MaxX -= 5;
  155.                                     Delay(10);
  156.                                     EraseRect(rp, win->BorderLeft, win->BorderTop,
  157.                                       win->Width - win->BorderRight, win->Height - win->BorderBottom);
  158.                                   }
  159.  
  160.                                 TT_CloseFont(font);
  161.                               }
  162.                             else PutStr("Font open failed.\n");
  163.  
  164.                             sigmask = SIGBREAKF_CTRL_C | (1 << win->UserPort->mp_SigBit);
  165.                             while (running)
  166.                               {
  167.                                 signals = Wait(sigmask);
  168.                                 if (signals & SIGBREAKF_CTRL_C) running = FALSE;
  169.                                 if (signals & (1 << win->UserPort->mp_SigBit))
  170.                                   {
  171.                                     struct IntuiMessage *imsg;
  172.  
  173.                                     while (imsg = (struct IntuiMessage*)GetMsg(win->UserPort))
  174.                                       {
  175.                                         if (imsg->Class == IDCMP_CLOSEWINDOW) running = FALSE;
  176.                                         ReplyMsg((struct Message*)imsg);
  177.                                       }
  178.                                   }
  179.                               }
  180.                             ReleasePen(win->WScreen->ViewPort.ColorMap, Red);
  181.                             CloseWindow(win);
  182.                           }
  183.                         CloseLibrary(TTEngineBase);
  184.                       }
  185.                     free_font_name(fontname);
  186.                   }
  187.                 CloseLibrary(AslBase);
  188.               }
  189.             CloseLibrary(IntuitionBase);
  190.           }
  191.         CloseLibrary(GfxBase);
  192.       }
  193.     return 0;
  194.   }
  195.